<?php

namespace app\common\model;

use think\Model;

/**
 * 分类模型
 */
class LitestoreCategory extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [

    ];

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $row->save(['weigh' => $row['id']]);
        });
    }

    /**
     * 读取分类列表
     * @param string $status 指定状态
     * @return array
     */
    public static function getCategoryArray($status = null)
    {
        $list = collection(self::order('weigh', 'desc')->select())->toArray();
        return $list;
    }

    /**
     * 获取上级分类,以及上级的上级
     * @param string $pid
     */
    public static function get_parent($pid){
        $str = '';
        $p1 = self::where('id', '=', $pid)->find()->toArray();
        if($p1['pid'] > 0){
            $p2 = self::where('id', '=', $p1['pid'])->find()->toArray();
            if($p2['pid'] > 0){
                $p3 = self::where('id', '=', $p2['pid'])->find()->toArray();
                $str .= $p3['name'].'-'.$p2['name'].'-'.$p1['name'];
            }else{
                $str .= $p2['name'].'-'.$p1['name'];
            }
        }else{
            $str .= $p1['name'];
        }

        return $str;
    }

}
